package com.hw.ui.relation_info;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.hw.base.model.BaseModel;
import com.hw.daobean.DataBaseControl;
import com.hw.daobean.ProductInfo;
import com.hw.daobean.RelationInfo;
import com.hw.daobean.RelationUpInfo;
import java.util.ArrayList;
import java.util.List;

public class RelationInfoModelImpl extends BaseModel implements RelationInfoModel {
    private String tag = "RelationInfoModelImpl";
    private String msg="";
    List<ProductInfo> list = new ArrayList<ProductInfo>();

    public RelationInfoModelImpl(Context context) {
        super(context);
    }

    public void postHandle(final CallBack callBack, final int n) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                switch (n) {
                    case -1:
                        break;
                    case 0:
                        callBack.onshowp(list);
                        break;
                    case 1:
                        callBack.onnotifyp(list);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    public void showp(CallBack callBack) {
        list = DataBaseControl.searchP();
        postHandle(callBack, 0);
    }

    @Override
    public void showpID(String pid,CallBack callBack) {
        list = DataBaseControl.searchPid(pid);
        postHandle(callBack, 0);
    }

    @Override
    public int getnum(String pid, String batch) {
        int a = caseTotalNum(DataBaseControl.getlist(pid,batch));
        return a;
    }

    @Override
    public int getUpnum(String pid, String batch) {
        int a = caseUpTotalNum(DataBaseControl.getUplist(pid,batch));
        return a;
    }

    private int caseTotalNum(List<RelationInfo> list)
    {
        int num = 0;
        String str="";
        for (int i = 0; i <list.size() ; i++) {
            if(!str.equals(list.get(i).getCasecode()))
            {
                num++;
                str = list.get(i).getCasecode();
            }
        }
        return num;
    }

    private int caseUpTotalNum(List<RelationUpInfo> list)
    {
        int num = 0;
        String str="";
        for (int i = 0; i <list.size() ; i++) {
            if(!str.equals(list.get(i).getCasecode()))
            {
                num++;
                str = list.get(i).getCasecode();
            }
        }
        return num;
    }
}

